package com.coffee.admin.controller.supplier;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.controller.BaseController;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.PlatformLoginCheck;
import com.coffee.core.anotation.SupplierLoginCheck;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.SkuTypeEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import com.coffee.daili.api.query.DailiMerchantSkuQuery;
import com.coffee.daili.api.query.DailiSkuQuery;
import com.coffee.daili.api.req.InsertSkuReq;
import com.coffee.daili.api.result.*;
import com.coffee.daili.api.service.DailiOrderService;
import com.coffee.daili.api.service.DailiSkuService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/supplier/daili")
public class SupplierDailiController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(SupplierDailiController.class);

    @DubboReference(check = false)
    private DailiSkuService dailiSkuService;
    @DubboReference(check = false)
    private DailiOrderService dailiOrderService;

    @OperLog(operModul = "供应商-系统管理-营销管理", operDesc = "分页查询账户管理列表")
    @SupplierLoginCheck(alias = "SupplierDailiSkuListAlias")
    @RequestMapping(value = "/sku/findList", method = {RequestMethod.POST})
    public Result<Page<DailiSkuResult>> findList(HttpServletRequest request, @RequestBody DailiSkuQuery params) {
        Result<Page<DailiSkuResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(params.getPageSize(), params.getPageIndex());
        Integer status = params.getStatus();
        if (status != null && status == 0) {
            params.setIsApprove("W");
        } else if (status != null && status == 1) {
            params.setIsApprove("N");
        } else if (status != null && status == 2) {
            params.setIsApprove("Y");
        } else if (status != null && status == 3) {
            params.setIsEnable("N");
        } else if (status != null && status == 4) {
            params.setIsEnable("Y");
        }
        params.setIsDisable("N");
        params.setSkuType(SkuTypeEnum.SKU.getCode());
        params.setSupplierId(getSupplier().getSupplierId());
        params.setIsDelete("N");
        result.setData(dailiSkuService.getPager(params, pagerInfo));
        return result;
    }


    @OperLog(operModul = "供应商-系统管理-营销管理", operDesc = "分页查询账户管理列表")
    @SupplierLoginCheck(alias = "SupplierDailiSkuOperationAlias")
    @RequestMapping(value = "/sku/insert", method = {RequestMethod.POST})
    public Result<String> skuInsert(HttpServletRequest request, @RequestBody InsertSkuReq req) {
        Result<String> result = Result.success(null);
        try {
            req.setSupplierId(getSupplier().getSupplierId());
            dailiSkuService.insert(req);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }


    @OperLog(operModul = "供应商-系统管理-营销管理", operDesc = "分页查询账户管理列表")
    @SupplierLoginCheck(alias = "SupplierDailiSkuOperationAlias")
    @RequestMapping(value = "/sku/update", method = {RequestMethod.POST})
    public Result<String> skuInsert(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<String> result = Result.success(null);
        Double proportion = jsonObject.getDouble("proportion");
        Integer id = jsonObject.getInteger("id");
        try {
            DailiSkuResult dailiSkuResult = new DailiSkuResult();
            dailiSkuResult.setId(id);
            dailiSkuResult.setProportion(proportion);
            dailiSkuService.addOrUpdate(dailiSkuResult);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "供应商-系统管理-营销管理", operDesc = "分页查询账户管理列表")
    @SupplierLoginCheck(alias = "SupplierDailiSkuOperationAlias")
    @RequestMapping(value = "/sku/delete", method = {RequestMethod.POST})
    public Result<String> skuDelete(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<String> result = Result.success(null);
        Integer id = jsonObject.getInteger("id");
        dailiSkuService.delete(id);
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "供应商-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @SupplierLoginCheck(alias = "SupplierDailiOrderListAlias")
    @RequestMapping(value = "/order/merchantDetail", method = {RequestMethod.POST})
    public Result<DailiMerchantResult> orderMerchantDetail(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<DailiMerchantResult> result = Result.success(null);
        String date = jsonObject.getString("date");
        DailiMerchantResult dailiMerchatResult = dailiOrderService.getDailiMerchatResult(getSupplier().getSupplierId(), date);
        result.setData(dailiMerchatResult);
        return result;
    }

    @OperLog(operModul = "供应商-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @SupplierLoginCheck(alias = "SupplierDailiOrderListAlias")
    @RequestMapping(value = "/order/fenxiaoyuanList", method = {RequestMethod.POST})
    public Result<Page<FenxiaoYuanList>> orderfenxiaoyuanList(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<Page<FenxiaoYuanList>> result = Result.success(null);
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        Page<FenxiaoYuanList> fenxiaoYuanList = dailiOrderService.getFenxiaoYuanList(getSupplier().getSupplierId(), null, pagerInfo);
        result.setData(fenxiaoYuanList);
        return result;
    }

    @OperLog(operModul = "供应商-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @SupplierLoginCheck(alias = "SupplierDailiOrderListAlias")
    @RequestMapping(value = "/order/shanghuList", method = {RequestMethod.POST})
    public Result<Page<ShanghuListResult>> ordershanghuList(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<Page<ShanghuListResult>> result = Result.success(null);
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        Page<ShanghuListResult> shanghuListResult = dailiOrderService.getShanghuListResult(getSupplier().getSupplierId(), pagerInfo);
        result.setData(shanghuListResult);
        return result;
    }

    @OperLog(operModul = "供应商-系统管理-城市合伙人", operDesc = "分页查询账户管理列表")
    @SupplierLoginCheck(alias = "SupplierDailiOrderListAlias")
    @RequestMapping(value = "/order/list", method = {RequestMethod.POST})
    public Result<Page<DailiOrderListResult>> orderList(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Result<Page<DailiOrderListResult>> result = Result.success(null);
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String date = jsonObject.getString("date");
        PagerInfo pagerInfo = setDefaultPageInfo(pageSize, pageIndex);
        Page<DailiOrderListResult> dailiOrderListResult = dailiOrderService.getDailiOrderListResult(getSupplier().getSupplierId(), date, pagerInfo);
        result.setData(dailiOrderListResult);
        return result;
    }

}
